package com.hc.demo.controller;

import com.hc.common.bean.Result;
import com.hc.common.bean.ResultUtil;
import com.hc.demo.domain.MyBatisDept;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hc
 * @date 2021/12/24 17:51
 */
@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {

    @GetMapping("dispatch/{index}")
    public String dispatch(@PathVariable("index") String index) {
        return index;
    }

    @ResponseBody
    @GetMapping("/fun1")
    public Result fun1() {
        MyBatisDept dept = new MyBatisDept(10, "sales", "CHICAGO");
        return ResultUtil.success("根据id查询成功").setData(dept);
    }

    @ResponseBody
    @GetMapping("/fun2")
    public Result fun2() {
        List<MyBatisDept> depts = new ArrayList<>();
        depts.add(new MyBatisDept(10, "ACCOUNTING", "NEWYORK"));
        depts.add(new MyBatisDept(20, "RESEARCH", "DALLAS"));
        depts.add(new MyBatisDept(30, "SALES", "CHICAGO"));
        depts.add(new MyBatisDept(40, "OPERATIONS", "BOSTON"));

        return ResultUtil.success("OK!!!!!!!").setData("depts", depts);
    }

    @GetMapping("/fun3")
    public ModelAndView fun3() {
        ModelAndView mav = new ModelAndView("index3");

        mav.addObject("dept", (new MyBatisDept(10, "sales", "CHICAGO")));
        return mav;
    }

    @GetMapping("/fun4")
    public ModelAndView fun4(ModelAndView mav) {
        mav.setViewName("index4");

        List<MyBatisDept> depts = new ArrayList<>();
        depts.add(new MyBatisDept(10, "ACCOUNTING", "NEWYORK"));
        depts.add(new MyBatisDept(20, "RESEARCH", "DALLAS"));
        depts.add(new MyBatisDept(30, "SALES", "CHICAGO"));
        depts.add(new MyBatisDept(40, "OPERATIONS", "BOSTON"));

        mav.addObject("depts", depts);
        return mav;
    }

}
